package com.leo.foodmaster.admin.stock.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.stock.entity.WarehouseStock;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * 仓库库存Mapper
 * </p>
 *
 * @author ：Leo
 * @since ：2021/9/19 18:21
 */
public interface WarehouseStockMapper extends BaseMapper<WarehouseStock> {
    /**
     * 条件分页查询仓库库存
     * @param page 分页条件
     * @param organId 公司Id
     * @param warehouseId 仓库Id
     * @param foodMaterialName 食材名称
     * @param foodMaterialTypeId 食材Id
     * @return IPage<WarehouseStock>
     */
    @Select("<script>" +
            "SELECT temp.warehouse_in_record_details_id, temp.organ_id, temp.warehouse_id, temp.warehouse_name, temp.warehouse_serial_num, temp.unit_name, temp.brand, " +
            "temp.food_material_id, temp.food_material_name, temp.food_material_type_id,  temp.food_material_type_name, " +
            "temp.specs, temp.price, " +
            "(SUM(warehouseInAmount) - SUM(warehouseOutAmount)) AS stock " +
            "FROM " +
            "( " +
            "SELECT a.id AS warehouse_in_record_details_id, b.organ_id, a.warehouse_id, a.warehouse_name, a.warehouse_serial_num, a.unit_name, a.brand, " +
            "a.food_material_id, a.food_material_name, a.food_material_type_id, a.food_material_type_name, " +
            "a.specs, a.price, a.warehouse_in_amount AS warehouseInAmount, 0 AS warehouseOutAmount " +
            "FROM warehouse_in_record_details AS a " +
            "JOIN warehouse_in_record AS b " +
            "ON a.warehouse_in_record_id = b.id " +
            "WHERE b.`status` = '2' " +
            "<if test='organId != null '>" +
            "AND b.organ_id = #{organId} " +
            "</if>" +

            "UNION ALL " +

            "SELECT a.warehouse_in_record_details_id, b.organ_id, a.warehouse_id, a.warehouse_name, a.warehouse_serial_num, a.unit_name, a.brand, " +
            "a.food_material_id, a.food_material_name, a.food_material_type_id,  a.food_material_type_name, " +
            "a.specs, a.price, 0 AS warehouseInAmount, a.warehouse_out_amount AS warehouseOutAmount " +
            "FROM warehouse_out_record_details AS a " +
            "JOIN warehouse_out_record AS b " +
            "ON a.warehouse_out_record_id = b.id " +
            "WHERE 1 = 1 " +
            "<if test='organId != null '>" +
            "AND b.organ_id = #{organId} " +
            "</if>" +
            ") AS temp " +
            "WHERE 1 = 1 " +

            "<if test='warehouseId != null '>" +
            "AND temp.warehouse_id = #{warehouseId} " +
            "</if>" +

            "<if test='foodMaterialName != null and foodMaterialName != &quot;&quot; '>" +
            "AND temp.food_material_name like concat('%', #{foodMaterialName}, '%') " +
            "</if>" +

            "<if test='foodMaterialTypeId != null '>" +
            "AND temp.food_material_type_id = #{foodMaterialTypeId} " +
            "</if>" +

            "GROUP BY  " +
            "temp.warehouse_in_record_details_id, " +
            "temp.organ_id, " +
            "temp.warehouse_id, " +
            "temp.warehouse_name, " +
            "temp.warehouse_serial_num, " +
            "temp.unit_name, " +
            "temp.brand, " +
            "temp.food_material_id, " +
            "temp.food_material_name, " +
            "temp.food_material_type_id, " +
            "temp.food_material_type_name, " +
            "temp.specs, " +
            "temp.price " +
            "</script>")
    IPage<WarehouseStock> getWarehouseStockPageList(Page<Object> page, Long organId,
                                                    Long warehouseId, String foodMaterialName,
                                                    Long foodMaterialTypeId);
}
